package com.dalin.platform.loa.action;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.dalin.common.utils.StringUtils;
import com.dalin.framework.action.StrutsTwoAction;
import com.dalin.framework.vos.CheckboxTreeVo;
import com.dalin.framework.vos.TreeVo;
import com.dalin.platform.loa.condition.LoaOrganizationCondition;
import com.dalin.platform.loa.condition.LoaUserCondition;
import com.dalin.platform.loa.model.LoaOrganizationEntity;
import com.dalin.platform.loa.model.LoaRoleEntity;
import com.dalin.platform.loa.model.LoaUserEntity;
import com.dalin.platform.loa.service.LoaService;

/*****************************************************************************
 * @content: 查询用户授权树,首层是机构信息，机构下面是人,带查询条件 ,右面显示详细信息                 *
 * @author : 那建林                                                                                                                                 *
 * @crtDate: 2010-06-07                                                     *
 ****************************************************************************/
public class RoleUserQueryAction extends StrutsTwoAction {
	private LoaService loaService;
	private Long organParentId;
	private Long roleId;
	
	public String execute() throws Exception {
		super.printInfo("", "===== this action begin");
		List<TreeVo> treeList = new ArrayList<TreeVo>();
		LoaOrganizationCondition organCond = new LoaOrganizationCondition();
		organCond.setProjectId(getCurrentUser().getProjectId());
		organCond.setParentId(organParentId);
		List<LoaOrganizationEntity> organList = loaService
						.queryListByCond(organCond.prepareParams());
		// 组织机构判断是否为叶子结点,加载人
		if (StringUtils.isBlank(organList)) {
			LoaUserCondition userCond = new LoaUserCondition();
			userCond.setOrganId(organParentId);
			// 设查询条件
			List<LoaUserEntity> userList = loaService
					.queryListByCond(userCond.prepareParams());
			CheckboxTreeVo ctv = null;
			for (LoaUserEntity lue : userList) {
				ctv = new CheckboxTreeVo();
				ctv.setId(lue.getUserId()+"");
				ctv.setText(lue.getRealName());
				ctv.setLeaf(true);
				ctv.setChecked(isRoleOwningUser(roleId, lue));
				treeList.add(ctv);
			}
		} else {
			TreeVo tv = null;
			for (LoaOrganizationEntity organEnty : organList) {
				tv = new TreeVo();
				tv.setId(organEnty.getOrganId() + "");
				tv.setText(organEnty.getOrganName());
				treeList.add(tv);
			}
		}
		setResContent(treeList);
		return null;
	}

	// 某角色是否拥有该用户权限
	private boolean isRoleOwningUser(Long roleId, LoaUserEntity userEnty) {
		LoaRoleEntity roleEnty = null;
		Set roleEntySet = userEnty.getLoaRoleEntitys();
		for (Iterator it = roleEntySet.iterator(); it.hasNext();) {
			roleEnty = (LoaRoleEntity) it.next();
			if (roleId.equals(roleEnty.getRoleId())) {
				return true;
			}
		}
		return false;
	}
	
	
	
	/*************************************************************************
	 * 以下是bean属性的set/get方法
	 */
	public LoaService getLoaService() {
		return loaService;
	}

	public void setLoaService(LoaService loaService) {
		this.loaService = loaService;
	}

	public Long getOrganParentId() {
		return organParentId;
	}

	public void setOrganParentId(Long organParentId) {
		this.organParentId = organParentId;
	}

	public Long getRoleId() {
		return roleId;
	}

	public void setRoleId(Long roleId) {
		this.roleId = roleId;
	}

	
}
